package org.jsola.hr.service.appro.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.approval.vo.ApproModuleContext;
import org.jsola.hr.constant.TransferAccountsSubjectStatusEnum;
import org.jsola.hr.entity.TransferAccountsBatchDO;
import org.jsola.hr.service.ITransferAccountsBatchService;
import org.jsola.hr.service.appro.AbstractApproListener;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.jsola.hr.constant.HrConstants.APPROVE_ONE_KEY_PAY;
import static org.jsola.hr.constant.HrConstants.SUBJECT_TYPE_ONE_KEY_PAY;

/**
 * 一键发薪审批回调
 *
 * @author wcl
 */
@Slf4j
@Component
public class TransferAccountsBatchApproListener extends AbstractApproListener {

    @Autowired
    ITransferAccountsBatchService transferAccountsBatchService;

    @Override
    public ApproModuleContext passAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_ONE_KEY_PAY.equals(subjectType)) {
            TransferAccountsBatchDO accountsBatchDO = transferAccountsBatchService.selectDOById(subjectId,
                    tokenUser.getSiteId());
            // 设置通过时的审批状态
            accountsBatchDO.setSubjectStatus(TransferAccountsSubjectStatusEnum.WAIT_ISSUE.getValue());
            transferAccountsBatchService.updateByIdSelective(accountsBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("发新申请审批通过:转账批次id:{},流程id:{}", subjectId, approModuleContext.getFlowId());
        }
        return approModuleContext;
    }

    @Override
    public ApproModuleContext rejectAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_ONE_KEY_PAY.equals(subjectType)) {
            TransferAccountsBatchDO accountsBatchDO = transferAccountsBatchService.selectDOById(subjectId,
                    tokenUser.getSiteId());
            accountsBatchDO.setSubjectStatus(TransferAccountsSubjectStatusEnum.APPROVE_REJECT.getValue());
            transferAccountsBatchService.updateByIdSelective(accountsBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("发新申请审批拒绝:转账批次id:{},流程id:{}", subjectId, approModuleContext.getFlowId());

        }
        return approModuleContext;
    }

    @Override
    public ApproModuleContext revokeAppro(ApproModuleContext approModuleContext) {
        TokenUser tokenUser = approModuleContext.getTokenUser();
        String subjectType = approModuleContext.getSubjectType();
        String subjectId = approModuleContext.getSubjectId();
        if (SUBJECT_TYPE_ONE_KEY_PAY.equals(subjectType)) {
            TransferAccountsBatchDO accountsBatchDO = transferAccountsBatchService.selectDOById(subjectId,
                    tokenUser.getSiteId());
            accountsBatchDO.setSubjectStatus(TransferAccountsSubjectStatusEnum.APPROVE_REJECT.getValue());
            transferAccountsBatchService.updateByIdSelective(accountsBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
            log.info("发新申请审批撤销:转账批次id:{},流程id:{}", subjectId, approModuleContext.getFlowId());

        }
        return approModuleContext;
    }

    @Override
    public String getSubjectType() {
        return SUBJECT_TYPE_ONE_KEY_PAY;
    }

    @Override
    public String getCode(ApproModuleContext approModuleContext) {
        return APPROVE_ONE_KEY_PAY;
    }
}
